package com.cma.service.impl;

import com.cma.model.db.Chntpcloneclassinfo;
import com.cma.dao.ChntpcloneclassinfoRepository;
import com.cma.model.param.TfDictionaryParam;
import com.cma.service.ChntpcloneclassinfoService;

        import org.apache.commons.logging.Log;
        import org.apache.commons.logging.LogFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

        import java.util.ArrayList;
        import java.util.List;


/**
 * 中国热带气旋分级信息表
 * 数据库表【chntpcloneclassinfo】对应的service实现类
 *
 * @author  bjs
 * @date 2017-01-03 18:59:17
 */
@Service
public class ChntpcloneclassinfoServiceImpl implements ChntpcloneclassinfoService {

    private static final Log logger = LogFactory.getLog(ChntpcloneclassinfoServiceImpl.class);

    @Autowired
    private ChntpcloneclassinfoRepository chntpcloneclassinfoRepository;


    @PersistenceContext
    private EntityManager entityManager;


    //===================================================================================================

    /**
     * 保存或更新对象
     *
     * @param chntpcloneclassinfo
     */
    @Override
    public void save(Chntpcloneclassinfo chntpcloneclassinfo) {
        chntpcloneclassinfoRepository.save(chntpcloneclassinfo);
    }


    /**
         * 通过id查找对象
         *
         * @param id
         * @return
         */
    @Override
    public Chntpcloneclassinfo findById(Long id) {
        return chntpcloneclassinfoRepository.findOne(id);
    }

    /**
     * 获取全部台风等级信息字典
     *
     * @return
     */
    @Override
    public List<Chntpcloneclassinfo> findAll() {
        ArrayList<Chntpcloneclassinfo> list= (ArrayList<Chntpcloneclassinfo>) chntpcloneclassinfoRepository.findAll();
        return list;
    }

    /**
     * 根据英文名称获取中文名称
     * @param tfDictionaryParam
     * @return
     */
    @Override
    public Chntpcloneclassinfo findByEnName(TfDictionaryParam tfDictionaryParam) {
        StringBuffer sql=new StringBuffer("select a from Chntpcloneclassinfo a where a.cLevelnameEn='"+tfDictionaryParam.getTfClassEnnname()+"'");
        Query query=entityManager.createQuery(sql.toString(),Chntpcloneclassinfo.class);
        List<Chntpcloneclassinfo> list =query.getResultList();
        if(list!=null&&list.size()>0){
            return list.get(0);
        }
        return null;
    }


    /**
    * 提供给管理使用的分页查询
    *
    * @param pageable
    * @return
    */
    @Override
    public Page<Chntpcloneclassinfo> findForAdminList(Pageable pageable) {
        StringBuffer sqlForCount = new StringBuffer("select count(c.id) from Chntpcloneclassinfo c where 1=1 ");
        StringBuffer sqlForList = new StringBuffer("select c from Chntpcloneclassinfo c where 1=1 ");

        /*
        if (StringUtils.isNotBlank(name)) {
            sqlForCount.append(" and ( c.name like  '%" + name + "%' or c.username like '%" + name + "%' )  ");
            sqlForList.append(" and ( c.name like  '%" + name + "%' or c.username like '%" + name + "%' )  ");
        }
        */

        sqlForList.append(" order by c.id desc ");

        TypedQuery queryForCount = entityManager.createQuery(sqlForCount.toString(), Long.class);
        Long count = (Long) queryForCount.getSingleResult();

        TypedQuery queryForList = entityManager.createQuery(sqlForList.toString(), Chntpcloneclassinfo.class);
        List<Chntpcloneclassinfo> adminList = queryForList.setMaxResults(pageable.getPageSize()).setFirstResult(pageable.getOffset()).getResultList();

        Page<Chntpcloneclassinfo> pageRst = new PageImpl<Chntpcloneclassinfo>(adminList, pageable, count);
        return pageRst;
    }



}
